<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>添加学科</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: bold;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn-group {
            margin-top: 20px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        .btn-primary {
            background-color: #1ab394;
            color: white;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        .btn-default {
            background-color: #f3f3f4;
            color: #333;
        }
        .btn-default:hover {
            background-color: #e7eaec;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2><i class="fas fa-plus-circle"></i> 添加学科</h2>
        <form id="subjectForm" action="${pageContext.request.contextPath}/admin/subject/add" method="post">
            <div class="form-group">
                <label for="subjectName">学科名称：</label>
                <input type="text" id="subjectName" name="subjectName" class="form-control" required>
            </div>
            
            <input type="hidden" name="teacherId" value="${sessionScope.adminUser.userId}">
            
            <div class="form-group">
                <label for="classroom">教室：</label>
                <input type="text" id="classroom" name="classroom" class="form-control" required>
            </div>
            
            <div class="form-group">
                <label for="weekDay">上课时间：</label>
                <div style="display: flex; gap: 10px;">
                    <select id="weekDay" name="weekDay" class="form-control" required>
                        <option value="">请选择星期</option>
                        <option value="1">星期一</option>
                        <option value="2">星期二</option>
                        <option value="3">星期三</option>
                        <option value="4">星期四</option>
                        <option value="5">星期五</option>
                        <option value="6">星期六</option>
                        <option value="7">星期日</option>
                    </select>
                    <select id="classTime" name="classTime" class="form-control" required>
                        <option value="">请选择时间</option>
                        <option value="08:00">08:00</option>
                        <option value="09:00">09:00</option>
                        <option value="10:00">10:00</option>
                        <option value="11:00">11:00</option>
                        <option value="14:00">14:00</option>
                        <option value="15:00">15:00</option>
                        <option value="16:00">16:00</option>
                        <option value="17:00">17:00</option>
                    </select>
                </div>
            </div>
            
            <div class="btn-group">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-default" onclick="history.back()">
                    <i class="fas fa-arrow-left"></i> 返回
                </button>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('subjectForm').onsubmit = function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const params = new URLSearchParams();
            for (let pair of formData.entries()) {
                params.append(pair[0], pair[1]);
            }
            
            fetch(this.action, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: params
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('添加成功');
                    window.location.href = '${pageContext.request.contextPath}/admin/subject/list';
                } else {
                    alert(data.message || '添加失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('添加失败');
            });
        };
    </script>
</body>
</html> 